package com.wef.demo.action;

import java.math.BigDecimal;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gfa4j.query.Page;
import com.gfa4j.query.QueryInfo;
import com.gfa4j.resource.ResourceManagerServer;
import com.gfa4j.wef.common.action.CommonQueryAction;
import com.gfa4j.wef.struts.BaseAction;
import com.gfa4j.wef.struts.QueryHandle;
import com.wef.demo.entity.Clazz;
import com.wef.demo.entity.domain.ClazzDomain;

public class ClazzAction extends BaseAction {

	public ActionForward query1(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 1.
		QueryInfo queryInfo = CommonQueryAction.getQueryInfo(request);
		// 2.
		Page page = (Page) QueryHandle.findByQuery(queryInfo);

		// 3.页脚汇总数据
		JSONObject userdataJsonObj = new JSONObject();
		// 将页脚的lesder 字段用于显示"班费统计"
		userdataJsonObj.put(ClazzDomain.lesder, ResourceManagerServer.getInstance().getMessage("demo.Clazz.FeeTotal"));

		BigDecimal totalFee = new BigDecimal(0);
		for (Iterator<Clazz> iterator = page.getList().iterator(); iterator.hasNext();) {
			Clazz clazz = (Clazz) iterator.next();
			if (clazz.getFee() != null && clazz.getFee().compareTo(new BigDecimal(0)) == 1)
				totalFee = totalFee.add(clazz.getFee());
		}
		// 将页脚的fee 的显示统计数据
		userdataJsonObj.put(ClazzDomain.fee, totalFee);

		// 4.
		String jsonString = CommonQueryAction.page2JsonString(page, request, queryInfo, userdataJsonObj);
		// 5.
		CommonQueryAction.outData2Page(jsonString, response);
		return null;
	}
}
